package com.woniuxy.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.woniuxy.core.util.Result;
import com.woniuxy.core.util.StateNum;
import com.woniuxy.goods.dto.GoodsCatDto;
import com.woniuxy.goods.entity.Brand;
import com.woniuxy.goods.entity.GoodsBrandCat;
import com.woniuxy.goods.entity.GoodsCat;
import com.woniuxy.goods.form.AddGoodsCatForm;
import com.woniuxy.goods.mapper.BrandMapper;
import com.woniuxy.goods.mapper.GoodsBrandCatMapper;
import com.woniuxy.goods.param.AddBrandCatParam;
import com.woniuxy.goods.param.AddGoodsCatParam;
import com.woniuxy.goods.param.UpdateBrandCatParam;
import com.woniuxy.goods.service.BrandService;
import com.woniuxy.goods.service.GoodsBrandCatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.goods.service.GoodsCatService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 * 品牌、类别绑定服务层实现类
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class GoodsBrandCatServiceImpl extends ServiceImpl<GoodsBrandCatMapper, GoodsBrandCat> implements GoodsBrandCatService {

    /**
     * 品牌服务层接口
     */
    @Resource
    private BrandService brandService;
    /**
     * 类别服务层接口
     */
    @Resource
    private GoodsCatService catService;

    /**
     * 绑定典当物品品牌类别
     * @param param 新增品牌类别绑定对象
     * @return 品牌类别信息集合
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public List<GoodsBrandCat> addBrandCatConfig(AddBrandCatParam param) throws Exception {
        Brand brand = brandService.findBrandById(param.getBrandId());
        if (brand==null) throw new Exception("品牌异常");
        List<GoodsCat> cats = catService.findOneCatByIds(param.getCatIds());
        if (cats==null||cats.size()!=param.getCatIds().size()) throw new Exception("一级类别异常");
        QueryWrapper<GoodsBrandCat> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",param.getBrandId());
        wrapper.in("cat_id",param.getCatIds());
        List<GoodsBrandCat> brandCats = list(wrapper);
        if (ObjectUtil.isNotEmpty(brandCats)) throw new Exception("已存在对应品牌、类别绑定信息");
        List<GoodsBrandCat> brandCatList = new ArrayList<>();
        for (Integer catId : param.getCatIds()) {
            GoodsBrandCat brandCat = new GoodsBrandCat();
            brandCat.setCatId(catId);
            brandCat.setBrandId(param.getBrandId());
            save(brandCat);
            brandCatList.add(brandCat);
        }
        return brandCatList;
    }

    /**
     * 解绑典当物品品牌类别
     * @param ids 品牌、类别绑定信息id
     * @return
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public void delBrandCatConfig(Set<Integer> ids) {
        remove(new QueryWrapper<GoodsBrandCat>().in("id",ids));
    }

    /**
     * 换绑典当物品品牌类别
     * @param param 修改品牌类别绑定对象
     * @return 品牌类别信息
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public GoodsBrandCat updateBrandCatConfig(UpdateBrandCatParam param) throws Exception {
        GoodsBrandCat brandCat = getById(param.getId());
        if (brandCat==null) throw new Exception("该绑定信息不存在");
        GoodsCat cat = catService.findCatById(param.getCatId());
        if (cat==null||cat.getIsShow()== StateNum.CAT_STATE_DISABLED||cat.getUpCatId()!=0) throw new Exception("一级类别异常");
        QueryWrapper<GoodsBrandCat> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",brandCat.getBrandId());
        wrapper.eq("cat_id",param.getCatId());
        GoodsBrandCat query = getOne(wrapper);
        if (ObjectUtil.isNotEmpty(query)) throw new Exception("该品牌已与该类别绑定");
        brandCat.setCatId(param.getCatId());
        updateById(brandCat);
        return brandCat;
    }

    /**
     * 通过品牌id换绑典当物品品牌类别
     * @param param 新增品牌类别绑定对象
     * @return 品牌类别信息集合
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public List<GoodsBrandCat> updateBrandCatByBrandId(AddBrandCatParam param) throws Exception {
        remove(new QueryWrapper<GoodsBrandCat>().eq("brand_id",param.getBrandId()));
        return addBrandCatConfig(param);
    }

    /**
     * 通过典当物品品牌id查绑定的类别ids
     * @param brandId 品牌id
     * @return 类别id集合
     * @throws Exception
     */
    @Override
    public Set<Integer> findCatIdsByBrandId(Integer brandId) {
        QueryWrapper<GoodsBrandCat> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",brandId);
        List<GoodsBrandCat> list = list(wrapper);
        Set<Integer> catIds = new HashSet<>();
        for (GoodsBrandCat brandCat : list) {
            catIds.add(brandCat.getCatId());
        }
        return catIds;
    }

    /**
     * 通过典当物品类别id查绑定的品牌ids
     * @param catId 类别id
     * @return 品牌id集合
     * @throws Exception
     */
    @Override
    public Set<Integer> findBrandIdsByCatId(Integer catId) {
        QueryWrapper<GoodsBrandCat> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(catId)){
            wrapper.eq("cat_id",catId);
        }
        List<GoodsBrandCat> list = list(wrapper);
        Set<Integer> brandIds = new HashSet<>();
        for (GoodsBrandCat brandCat : list) {
            brandIds.add(brandCat.getBrandId());
        }
        return brandIds;
    }
}
